﻿using BookManage.BLL;
using BookManage.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BookManage
{
    public partial class frmPayBook : Form
    {
        Borrow borrow = new Borrow();
        Reader reader = new Reader();
        Book book = new Book();
        private DataTable dt = null;
        private BorrowAdmin borrowBLL = new BorrowAdmin();
        public frmPayBook()
        {
            InitializeComponent();
        }
        private void ShowBorrowData()
        {
            dgvBorrow.DataSource = dt;
            foreach (DataColumn dc in dt.Columns)
            {
                dgvBorrow.Columns[dc.ColumnName].HeaderText = Borrow.ColumnTitle(dc.ColumnName);
            }
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            MessageBox.Show("确定您借阅的图书已丢失？");
            //图书丢失，罚款金额为书原本价格
            if (txtrdID.Text == "" || txtbkID.Text == "" || txtPay.Text == "")
                MessageBox.Show("缴纳罚款请输入读者号和书号！！！");
            else 
            {
                int rdID = Convert.ToInt32(txtrdID.Text);
                int bkID = Convert.ToInt32(txtbkID.Text);
                float IdPunishMoney = Convert.ToSingle(txtPay.Text);
                book = BookAdmin.GetBook(bkID);
                if (book.bkPrice!= IdPunishMoney)
                {
                    MessageBox.Show("您缴纳的金额小于赔偿金额，请一次交清完毕！！！");
                    MessageBox.Show("应缴纳金额：" + Convert.ToString(book.bkPrice));
                }
                else
                {
                    borrow = BorrowAdmin.GetAllBorrow(rdID, bkID);//得到借阅记录
                    MessageBox.Show("账号：" + borrow.BorrowID);
                    // BorrowAdmin.DeleteBorrow(borrow);
                    BorrowAdmin.UpdatePay(rdID, bkID, IdPunishMoney);
                    BorrowAdmin.UpdateBookByLend(borrow);//更新读者表
                    BorrowAdmin.UpdateReaderByLend(borrow);//删除丢失图书
                    MessageBox.Show("您的罚款已缴清，记录已成功更新！！！");//更新借阅记录
                }

            }
        }

        private void SelectBk_Click(object sender, EventArgs e)
        {
            int rdID = Convert.ToInt32(txtrdID.Text);
            int bkID = Convert.ToInt32(txtbkID.Text);
            dt = borrowBLL.GetBorrow(bkID);
            ShowBorrowData();
            if(dt!= null&&dt.Rows.Count>0)
            {
                book = BookAdmin.GetBook(bkID);
                MessageBox.Show("您的应罚款金额为：" + book.bkPrice);
            }
        }
    }
}
